

package com.maoshi.shop.delivery.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.enums.OfflineHandleEventType;
import com.maoshi.shop.bean.model.OfflineHandleEvent;
import com.maoshi.shop.bean.param.OfflineHandleEventAuditParam;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.delivery.comment.model.Fetch;
import com.maoshi.shop.delivery.comment.service.FetchService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import com.maoshi.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 自提点
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/fetch" )
public class FetchController {

    private final FetchService fetchService;

    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fetch 自提点
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<Fetch>> getFetchPage(PageParam<Fetch> page, Fetch fetch) {
        return ResponseEntity.ok(fetchService.getPage(page, fetch));
    }


    /**
     * 通过id查询自提点
     * @param fetchId id
     * @return 单个数据
     */
    @GetMapping("/info/{fetchId}" )
    public ResponseEntity<Fetch> getById(@PathVariable("fetchId") Long fetchId) {
        return ResponseEntity.ok(fetchService.getById(fetchId));
    }
//
//    /**
//     * 新增自提点
//     * @param fetch 自提点
//     * @return 是否新增成功
//     */
//    @SysLog("新增自提点" )
//    @PostMapping
//    @PreAuthorize("@pms.hasPermission('platform:fetch:save')" )
//    public ResponseEntity<Boolean> save(@RequestBody @Valid Fetch fetch) {
//        fetch.setShopId(Constant.PLATFORM_SHOP_ID);
//        Date date = new Date();
//        fetch.setCreateTime(date);
//        fetch.setUpdateTime(date);
//        return ResponseEntity.ok(fetchService.save(fetch));
//    }
//
//    /**
//     * 修改自提点
//     * @param fetch 自提点
//     * @return 是否修改成功
//     */
//    @SysLog("修改自提点" )
//    @PutMapping
//    @PreAuthorize("@pms.hasPermission('platform:fetch:update')" )
//    public ResponseEntity<Boolean> updateById(@RequestBody @Valid Fetch fetch) {
//        fetch.setUpdateTime(new Date());
//        return ResponseEntity.ok(fetchService.updateById(fetch));
//    }
//
//    /**
//     * 通过id删除自提点
//     * @param fetchId id
//     * @return 是否删除成功
//     */
//    @SysLog("删除自提点" )
//    @DeleteMapping("/{fetchId}" )
//    @PreAuthorize("@pms.hasPermission('platform:fetch:delete')" )
//    public ResponseEntity<Boolean> removeById(@PathVariable Long fetchId) {
//        return ResponseEntity.ok(fetchService.removeById(fetchId));
//    }

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:fetch:update')" )
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Fetch fetch = fetchService.getById(offlineHandleEvent.getHandleId());

        if (fetch == null) {
            throw new MaoshiShopBindException("未找到该自提点信息");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        fetchService.offline(fetch, offlineHandleEvent.getOfflineReason(), sysUserId);

//        // 移除缓存
//        fetchService.removeSeckillCacheById(seckill.getSeckillId());
        return ResponseEntity.ok().build();
    }

    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByFetchId/{fetchId}")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByDiscountId(@PathVariable("fetchId") Long fetchId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.FETCH.getValue(), fetchId);
        return ResponseEntity.ok(offlineHandleEvent);
    }


    /**
     * 审核活动
     */
    @PostMapping("/auditFetch")
    @PreAuthorize("@pms.hasPermission('platform:fetch:update')")
    public ResponseEntity<Void> auditSeckill(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Fetch fetch = fetchService.getById(offlineHandleEventAuditParam.getHandleId());
        if (fetch == null) {
            throw new MaoshiShopBindException("未找到自提点信息");
        }
        fetchService.auditSeckill(offlineHandleEventAuditParam, sysUserId);

//        // 移除缓存
//        seckillService.removeSeckillCacheById(seckill.getSeckillId());
        return ResponseEntity.ok().build();
    }

}
